package cn.dwy2002.task.impl;

import cn.dwy2002.task.mapper.TaskTypeRelMapper;
import cn.dwy2002.task.service.ITaskTypeRelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dwy2002.common.exception.EmptyIdException;
import com.dwy2002.pojo.po.task.TaskTypeRel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.dwy2002.common.utils.Utils.isEmptyOrNull;
import static com.dwy2002.common.utils.Utils.isListEmpty;

/**
 * @author 杜伟毅
 * @version 1.0
 * @since 2025/07/14
 */
@Service
@RequiredArgsConstructor
public class TaskTypeRelServiceImpl extends ServiceImpl<TaskTypeRelMapper, TaskTypeRel> implements ITaskTypeRelService {

    /**
     * 添加任务类型关联
     *
     * @param taskId 任务ID
     * @param typeId 类型ID
     * @author 杜伟毅
     * @since 2025/7/22 0022 10:52
     */
    @Override
    public void addTypeToTask(String taskId, String typeId) {
        if (isEmptyOrNull(taskId) || isEmptyOrNull(typeId)) throw new EmptyIdException("addTypeToTask");

        LambdaQueryWrapper<TaskTypeRel> lqw = new LambdaQueryWrapper<>();
        lqw.eq(TaskTypeRel::getTaskId, taskId)
                .eq(TaskTypeRel::getTypeId, typeId);

        //查询数据为空才插入关联数据
        if (ObjectUtils.isEmpty(getOne(lqw))) {
            TaskTypeRel taskTypeRel = new TaskTypeRel();
            taskTypeRel.setTaskId(taskId);
            taskTypeRel.setTypeId(typeId);
            save(taskTypeRel);
        }
    }

    /**
     * 修改任务类型关联
     *
     * @param taskId 任务ID
     * @param typeId 类型ID
     * @author 杜伟毅
     * @since 2025/7/22 0022 10:52
     */
    @Override
    public void updateTypeToTask(String taskId, String typeId) {
        if (isEmptyOrNull(taskId) || isEmptyOrNull(typeId)) throw new EmptyIdException("updateTypeToTask");

        LambdaUpdateWrapper<TaskTypeRel> luw = new LambdaUpdateWrapper<>();
        luw.eq(TaskTypeRel::getTaskId, taskId)
                .eq(TaskTypeRel::getTypeId, typeId)
                .set(TaskTypeRel::getTypeId, typeId);
        update(luw);
    }

    /**
     * 删除任务类型关联
     *
     * @param taskIds 任务ID集合
     * @author 杜伟毅
     * @since 2025/7/22 0022 10:52
     */
    @Override
    public void removeTypeFromTask(List<String> taskIds) {
        if (isListEmpty(taskIds)) throw new EmptyIdException("removeTypeFromTask");
        LambdaQueryWrapper<TaskTypeRel> lqw = new LambdaQueryWrapper<>();
        lqw.in(TaskTypeRel::getTaskId, taskIds);
        remove(lqw);
    }
}
